package com.foredu.fiqs.web.action;

import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.PropertyUtils;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.foredu.fiqs.domain.Department;
import com.foredu.fiqs.domain.Role;
import com.foredu.fiqs.domain.Teacher;
import com.foredu.fiqs.domain.User;
import com.foredu.fiqs.service.IManagementService;
import com.foredu.fiqs.web.actionform.UserActionForm;

public class UserAction extends AbstractBaseDispatchAction {

	private IManagementService managementService = null;

	public IManagementService getManagementService() {
		return managementService;
	}

	public void setManagementService(IManagementService managementService) {
		this.managementService = managementService;
	}

	public ActionForward list(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		List<Teacher> users = managementService.findTeachers();
		List<Department> departMents = managementService.findDepartments();
		List<Role> roles = managementService.findRoles();
		request.setAttribute("users", users);
		request.setAttribute("deptList", departMents);
		request.setAttribute("roles", roles);
		return mapping.findForward("list");
	}

	public ActionForward add(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		List<Role> roles = managementService.findRoles();
		request.setAttribute("roles", roles);
		return mapping.findForward("add");
	}

	public ActionForward delete(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		UserActionForm uaf = (UserActionForm) form;
		managementService.deleteUserById(uaf.getId());
		List<Teacher> users = managementService.findTeachers();
		request.setAttribute("users", users);
		return mapping.findForward("delete");
	}

	public ActionForward toUpdate(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		UserActionForm uaf = (UserActionForm) form;
		User user = managementService.findUserById(uaf.getId());
		uaf.setId(user.getId());
		uaf.setUsername(user.getUsername());
		uaf.setPassword(user.getPassword());
		uaf.setNickname(user.getNickname());
		Set<Role> myroles = user.getRoles();
		if (null != myroles) {
			String[] roleIds = new String[myroles.size()];
			int i = 0;
			for (Iterator<Role> iterator = myroles.iterator(); iterator.hasNext();) {
				Role role = (Role) iterator.next();
				roleIds[i] = Integer.toString(role.getId());
				i++;
			}
			uaf.setRoleIds(roleIds);
		}
		List<Role> roles = managementService.findRoles();
		request.setAttribute("roles", roles);
		return mapping.findForward("toUpdate");
	}

	public ActionForward save(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		User user = new User();
		UserActionForm uaf = (UserActionForm) form;
		user.setUsername(uaf.getUsername());
		user.setPassword(uaf.getPassword());
		user.setNickname(uaf.getNickname());
		if (null != uaf.getRoleIds()) {
			Set<Role> roles = new HashSet<Role>();
			String[] roleIds = uaf.getRoleIds();
			for (int i = 0; i < roleIds.length; i++) {
				Role role = managementService.findRoleById(Integer
						.parseInt(roleIds[i]));
				roles.add(role);
			}
			user.setRoles(roles);
		} else {
			user.setRoles(null);
		}
		managementService.saveUser(user);
		List<User> users = managementService.findUsers();
		request.setAttribute("users", users);
		return mapping.findForward("save");
	}

	public ActionForward update(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		UserActionForm uaf = (UserActionForm) form;
		User user = managementService.findUserById(uaf.getId());
		PropertyUtils.copyProperties(user, uaf);
		if (null != uaf.getRoleIds()) {
			Set<Role> roles = new HashSet<Role>();
			String[] roleIds = uaf.getRoleIds();
			for (int i = 0; i < roleIds.length; i++) {
				Role role = managementService.findRoleById(Integer
						.parseInt(roleIds[i]));
				roles.add(role);
			}
			user.setRoles(roles);
		} else {
			user.setRoles(null);
		}
		managementService.updateUser(user);
		List<Teacher> users = managementService.findTeachers();
		request.setAttribute("users", users);
		
		return mapping.findForward("update");
	}

	//跳转到修改密码页面
	public ActionForward toUpdatePwd(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		return mapping.findForward("toUpdatePwd");
	}
	
	//完成密码修改操作
	public ActionForward updatePwd(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		User user = new User();
		UserActionForm uaf = (UserActionForm) form;
		user = managementService.findUserById(uaf.getId());
		user.setUsername(uaf.getUsername());
		user.setPassword(uaf.getPassword());
		user.setNickname(uaf.getNickname());
		managementService.updateUser(user);
		return mapping.findForward("updatePwd");
	}
}
